<?
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     * @author Kulikov Alexey <a.kulikov@gmail.com>
     */


    /**
     * Smarty {load_datablocks} function plugin
     *
     * Type:     function
     * Name:     load_datablocks
     * Purpose:  Loads Datablocks of a certain Node / Page
     *   @param array
     *   @param Smarty
     *
     * This smarty plugin can be talked to using the following
     * template parameters:
     *   
     *   * var (string) -- name of variable where to load the 
     *                     data tree to
     *                     >> default: stats
     *
     * @return string
     */
    function smarty_function_load_datablocks($params, &$smarty){
        global $db, $nodeData; // create a link to the database connection

        //pre-process parameters
        //variable name
        if(empty($params['var'])){
            $var = 'datablocks';
        }else{
            $var = $params['var'];
        }
        
        
        //cast tree
        $myTree         = new tree($db,'structure LEFT JOIN objectmap ON (structure.id = objectmap.nodeid)',null,'id, url, level, title');
        
        //get the full path to the page
        $path           = $myTree->getPathStack($nodeData['nodeid'],true);
        
        $blocks   = array();
        $myBlocks = array();
        foreach((array)$path as $value){
            $blocks = $db->getAssoc("SELECT 
                                        datablocks.tag AS key, 
                                        datablocks.id AS id, 
                                        datablocks.title AS title,
                                        datablocks.content AS content
                                    FROM datablock_node_mapping AS dnm, datablocks
                                    WHERE dnm.inherit = TRUE
                                    AND dnm.blockid = datablocks.id
                                    AND dnm.nodeid = ".$value['id']);
            
            $myBlocks = array_merge($myBlocks,$blocks);
        }

        if(empty($nodeData['pageref'])){
            $data = array_merge($myBlocks,$db->getAssoc("SELECT datablocks.tag, datablocks.id AS id, datablocks.title AS title, content
                                                            FROM datablock_node_mapping AS dnm, datablocks
                                                            WHERE dnm.blockid = datablocks.id
                                                            AND nodeid = ".(int)$nodeData['nodeid']));
        }else{
            $data = array_merge($myBlocks,$db->getAssoc("SELECT datablocks.tag, datablocks.id AS id, datablocks.title AS title, content  
                                                            FROM datablock_page_mapping AS dnm, datablocks
                                                            WHERE dnm.blockid = datablocks.id
                                                            AND pageid = ".(int)$nodeData['pageref']));
        }

        //give all this shit to smarty
        $smarty->assign($var,$data);
    }
?>